package stdio.costa.moca.rest;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import stdio.costa.moca.model.UserList;

import weibo4j.Account;
import weibo4j.Friendships;
import weibo4j.Users;
import weibo4j.model.User;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;

@Path("/friends")
public class FriendShipResources {

	@GET
	@Path("getFriendsList/{accessToken}")
	public UserList getFriendsList(@PathParam("accessToken") String accessToken) throws WeiboException, JSONException {

		System.out.println(accessToken);

		Account am = new Account();
		am.client.setToken(accessToken);

		JSONObject uid = am.getUid();

		System.out.println(uid.get("uid"));

		Friendships friends = new Friendships();

		friends.setToken(accessToken);

		String[] friendsIds = friends.getFriendsIdsByUid(uid.get("uid").toString());

		List<User> list = new ArrayList<User>();
		
		Users um = new Users();
		um.client.setToken(accessToken);
			
		for (int i = 0; i< 5 ; i++) {
			System.out.println(friendsIds[i]);
			User user;
			try {
				user = um.showUserById(friendsIds[i]);
				list.add(user);
			} catch (WeiboException e) {
				e.printStackTrace();
			}
		}
		
		UserList userList = new UserList();
		
		userList.setUserList(list);

		return userList;
	}
}
